package com.example.casper.Experiment2024.fragment;

import android.Manifest;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Looper;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.fragment.app.Fragment;

import com.alibaba.fastjson.JSON;
import com.example.casper.Experiment2024.R;
import com.example.casper.Experiment2024.activity.PublishActivity;
import com.example.casper.Experiment2024.entity.LocationInfo;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.tencent.map.geolocation.TencentLocation;
import com.tencent.map.geolocation.TencentLocationListener;
import com.tencent.map.geolocation.TencentLocationManager;
import com.tencent.map.geolocation.TencentLocationRequest;
import com.tencent.tencentmap.mapsdk.maps.CameraUpdateFactory;
import com.tencent.tencentmap.mapsdk.maps.TencentMap;
import com.tencent.tencentmap.mapsdk.maps.TextureMapView;
import com.tencent.tencentmap.mapsdk.maps.model.LatLng;
import com.tencent.tencentmap.mapsdk.maps.model.MarkerOptions;

import java.util.List;

public class TencentMapFragment extends Fragment {

    private static final String TAG = "TencentMapFragment";
    private TextureMapView mapView;
    private TencentMap tencentMap;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_tencent_map, container, false);
        mapView = view.findViewById(R.id.mapView);
        tencentMap = mapView.getMap();
        return view;
    }

    @Override
    public void onStart() {
        super.onStart();
        mapView.onStart();
    }

    @Override
    public void onResume() {
        super.onResume();
        mapView.onResume();
        SharedPreferences preferences = requireContext().getSharedPreferences("config", Context.MODE_PRIVATE);
        String json = preferences.getString("points", "[]");
        List<LocationInfo> infos = JSON.parseArray(json, LocationInfo.class);
        tencentMap.clearAllOverlays();

        {
            // 中心定位在暨南大学珠海校区
            LatLng point1 = new LatLng(22.255453, 113.54145);
            tencentMap.moveCamera(CameraUpdateFactory.newLatLng(point1));
            // 创建一个Marker对象
            MarkerOptions markerOptions = new MarkerOptions(point1).title("我是暨南大学珠海校区");
            // 添加标记到地图上
            tencentMap.addMarker(markerOptions);
        }

        for (LocationInfo info : infos) {
            LatLng point = new LatLng(info.getLat(), info.getLon());
            tencentMap.moveCamera(CameraUpdateFactory.newLatLng(point));
            MarkerOptions markerOptions = new MarkerOptions(point).tag(info).title(info.getTitle());
            tencentMap.addMarker(markerOptions);
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        mapView.onPause();
    }

    @Override
    public void onStop() {
        super.onStop();
        mapView.onStop();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mapView.onDestroy();
    }
}